/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.model;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author chuoxi
 */
public class SendJson {

    public int code = 200;
    /**
     * 错误描述可写 可不写 可集中在 客户端 判断 code 得出
     */
    public String msg;

    public Map data = new HashMap();

    public SendJson() {
    }

    public SendJson(int code) {

        this.code = code;
        this.data.put("code", code);
    }

    public SendJson(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data.put("msg", msg);
    }

    public String toJson() {
        String rst = JsonKit.toJson(data);
//		if (data!=null&&data.size() == 0) data = null;
//		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
//		String rst = gson.toJson(this);
//
//		/**
//		 * 只有一个 data 这时候就 去掉一层
//		 */
//		if (data != null && data.size() == 1) {
//			JSONObject json = JSONObject.parseObject(rst);
//			Object jsonData = json.getJSONObject("data").get("data");
//			if (jsonData != null) {
//				json.put("data", jsonData);
//				rst = json.toJSONString();
//			}
//		}

        return rst;
    }

    @Override
    public String toString() {
        return toJson();
    }

    public SendJson setData(String key, Object value) {
        if (value == null) {
            return this;
        }
        this.data.put(key, value);
        return this;
    }

}
